/*
* $Id: AddInspectWizard.java 272 2011-02-21 05:37:35Z volok-aleksej@yandex.ru $
*
* Copyright 2008-2011 by Volokitin Alexey
* All rights reserved
*
*/
package apiary.component.journal;

import java.util.Vector;

import apiary.Apiary;
import apiary.db.HousingStore.HousingRecord;
import apiary.db.managers.DBManager;
import apiary.ui.BackScreen;
import apiary.common.FormHelper;
import apiary.ui.journal.*;
import apiary.ui.journal.ChooseForm.ChooseValue;
import apiary.ui.journal.JournalForm.JournalValue;

/**
 * Wizard of adding inspect of family
 * @author lexus
 */
public class AddInspectWizard extends Wizard
{
    /**
     * Constructor
     */
    public AddInspectWizard() {
        super(null);
        Init();
    }

    /**
     * Constructor
     * @param parentForm Form where it want to move when pressing Back.
     * This form used only on first form of wizard
     */
    public AddInspectWizard(BackScreen parentForm) {
        super(null);
        Init();
        setCancelForm(parentForm);
    }

    /**
     * Saves result of wizard
     */
    public void save() {
        int idFamily = ((ChooseValue) getData(new Integer(1))).id;
        JournalValue value = (JournalValue) getData(new Integer(2));
        Vector inspects = (Vector) getData(new Integer(3));
        DBManager.getInstance().addInspect(idFamily, value, inspects);
    }

    /**
     * Calls before to show next form of wizard
     * @param nextForm next form of wizard
     */
    public void next(BackScreen nextForm) {
        WizardScreen form = (WizardScreen) nextForm;
        if(form.getIdForm().intValue() == 3)
            setHousingData((InspectHForm) form);
    }

    /**
     * Checks data of form on truth
     * @param dataForm data of form
     * @param idForm identificator of form
     * @return truth of data
     */
    public boolean isOk(Integer idForm, Object dataForm) {
        return true;
    }

    /**
     * Form is filled hives
     * @param form form
     */
    private void setHousingData(InspectHForm form) {
        form.deleteAll();
        ChooseValue family = (ChooseValue) getData(new Integer(1));
        Vector housings = DBManager.getInstance().getHousings(family.id);
        Vector inspects = (Vector) form.GetDataForm();
        for(int i = 0; i < housings.size(); i++) {
            HousingRecord housing = (HousingRecord) housings.elementAt(i);
            InspectValue inspect = new InspectValue();
            inspect.idHousing = housing.idHousing;
            inspect.nameHousing = housing.name;
            inspects.addElement(inspect);
        }
        form.setInspects(inspects);
    }

    /**
     * Initialize Wizard
     */
    private void Init() {
        Apiary apiary = Apiary.getInstance();
        Integer idChoose = new Integer(1);
        firstForm = new ChooseForm(apiary.getLocaleString("ChooseFamily"), idChoose, this, true);
        FormHelper.fillFamilyList((ChooseForm) firstForm);
        Integer idJournal = new Integer(2);
        JournalForm journal = new JournalForm(apiary.getLocaleString("AddInspect"), this, idJournal, true);
        journal.setBackScreen(firstForm);
        formsNext.put(idChoose, journal);
        Integer idInspectH = new Integer(3);
        InspectHForm inspectH = new InspectHForm(apiary.getLocaleString("InspectHousing"), this, idInspectH, false);
        inspectH.setBackScreen(journal);
        formsNext.put(idJournal, inspectH);
    }
}
